package com.start.mc;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity {
    private Button btnStart, btnStop;
    private EditText etToken, etServer, etStartSEC, etStopSEC, etStartCookie, etStopCookie;

    // 线程池，避免频繁创建线程
    private final ExecutorService executor = Executors.newSingleThreadExecutor();

    // SharedPreferences 名称
    private static final String PREFS_NAME = "MC_PREFS";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        loadInput(); // 加载之前保存的数据

        btnStart.setOnClickListener(v -> startServer());
        btnStop.setOnClickListener(v -> stopServer());
    }
    //初始化

    private void initViews(){
        btnStart=findViewById(R.id.start);
        btnStop=findViewById(R.id.stop);
        etToken=findViewById(R.id.token_edit);
        etServer=findViewById(R.id.server_edit);
        etStartSEC=findViewById(R.id.start_sec_edit);
        etStopSEC=findViewById(R.id.stop_sec_edit);
        etStartCookie=findViewById(R.id.start_cookie_edit);
        etStopCookie=findViewById(R.id.stop_cookie_edit);
    }

    /**
     * 校验启动输入是否完整
     */
    private boolean validateStartInputs() {
        if (isEmpty(etToken, "Token 为空")) return false;
        if (isEmpty(etServer, "Server 为空")) return false;
        if (isEmpty(etStartSEC, "StartSEC 为空")) return false;
        if (isEmpty(etStartCookie, "StartCookie 为空")) return false;
        return true;
    }

    /**
     * 校验停止输入是否完整
     */
    private boolean validateStopInputs() {
        if (isEmpty(etToken, "Token 为空")) return false;
        if (isEmpty(etServer, "Server 为空")) return false;
        if (isEmpty(etStopSEC, "StopSEC 为空")) return false;
        if (isEmpty(etStopCookie, "StopCookie 为空")) return false;
        return true;
    }


    /**
     * 通用非空检查
     */
    private boolean isEmpty(EditText editText, String msg) {
        if (editText.getText().toString().trim().isEmpty()) {
            Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
            return true;
        }
        return false;
    }


    /**
     * 启动服务器
     */
    private void startServer() {
        if (!validateStartInputs()) return;

        String token = etToken.getText().toString().trim();
        String server = etServer.getText().toString().trim();
        String sec = etStartSEC.getText().toString().trim();
        String cookie = etStartCookie.getText().toString().trim();

        executor.execute(() -> {
            String response = MCStart.start(token, server, sec, cookie);
            runOnUiThread(() -> Toast.makeText(this, response, Toast.LENGTH_LONG).show());
        });
    }


    /**
     * 停止服务器
     */
    private void stopServer() {
        if (!validateStopInputs()) return;

        String token = etToken.getText().toString().trim();
        String server = etServer.getText().toString().trim();
        String sec = etStopSEC.getText().toString().trim();
        String cookie = etStopCookie.getText().toString().trim();

        executor.execute(() -> {
            String response = MCStop.stop(token, server, sec, cookie);
            runOnUiThread(() -> Toast.makeText(this, response, Toast.LENGTH_LONG).show());
        });
    }

    /**
     * 保存 EditText 输入到 SharedPreferences
     */
    private void saveInput() {
        SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        prefs.edit()
                .putString("token", etToken.getText().toString().trim())
                .putString("server", etServer.getText().toString().trim())
                .putString("startSEC", etStartSEC.getText().toString().trim())
                .putString("stopSEC", etStopSEC.getText().toString().trim())
                .putString("startCookie", etStartCookie.getText().toString().trim())
                .putString("stopCookie", etStopCookie.getText().toString().trim())
                .apply(); // 异步保存
    }

    /**
     * 加载 SharedPreferences 中保存的输入
     */
    private void loadInput() {
        SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        etToken.setText(prefs.getString("token", ""));
        etServer.setText(prefs.getString("server", ""));
        etStartSEC.setText(prefs.getString("startSEC", ""));
        etStopSEC.setText(prefs.getString("stopSEC", ""));
        etStartCookie.setText(prefs.getString("startCookie", ""));
        etStopCookie.setText(prefs.getString("stopCookie", ""));
    }

    @Override
    protected void onPause() {
        super.onPause();
        saveInput(); // 页面离开时自动保存
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        executor.shutdownNow(); // 释放线程池资源
    }

}